/*
 * Copyright 2011 Nicolas Lochet Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 *	
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 */
package org.nicocube.gwt.parser.token.client;

import org.nicocube.gwt.parser.common.client.Content;

/**
 * This is a basic implementation of a {@link Token} based on a StringBuffer
 *
 * @author nicolas
 * @version 0.1
 */
public class TokenImpl<T extends TokenType> implements Token<T> {
	
	private T type;
	private Content content;
	private int begin;
	private int end;
	
	public TokenImpl(T type, Content content, int begin, int end) {
		this.type = type;
		this.content = content;
		this.begin = begin;
		this.end = end;
	}
	
	@Override
	public T getType() {
		return type;
	}

	@Override
	public int getBegin() {
		return begin;
	}

	@Override
	public int getEnd() {
		return end;
	}
	
	@Override
	public String getPortion() {
		try {
			return content.substring(begin, end);
		} catch (RuntimeException e) {
			// catch exception for debugging purpose, rethrow it immediately
			throw e;
		}
	}

	@Override
	public String toString() {
		return "{type:"+getType()+","+getBegin()+","+getEnd()+",'"+getPortion()+"'}";
	}
}
